﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WordService.Business.IBLL;
using WordService.Business;
using WordService.Model;
using System.Collections;
using WordService.Utility;

namespace WordLib.MatrixUI
{
    public partial class KeyWordPanel : UserControl
    {
        public KeyWordPanel()
        {
            InitializeComponent();
        }

        private Boolean _IsRate;
        private IList<Keyword> _OrgKeywordList;
        //private IList<Matrix> _OrgMatrixEle;
        //private Hashtable _MatrixMap = new Hashtable();
        private DataTable _ExcelTable = new DataTable();
        /// <summary>
        /// 是否带权值
        /// </summary>
        public Boolean IsRate
        {
            get { return _IsRate; }
            set { _IsRate = value; }
        }

        private IKeywordBLL keywordBLL = BLLManager.CreateKeywordBLL();
        /// <summary>
        /// 生成矩阵
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /*
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (_OrgKeywordList != null && _OrgKeywordList.Count > 0)
            {
                _OrgMatrixEle = new List<Matrix>();
                //FillMatrixMap(this._IsRate);
                // 转换Mapping为可以可见的List，最后计算的时候相加一次
                foreach (String key in _MatrixMap.Keys)
                {
                    Matrix matrix = new Matrix();
                    matrix.Name = key;
                    matrix.CellValue = Convert.ToDecimal(_MatrixMap[key]);
                    _OrgMatrixEle.Add(matrix);
                }
                // 计算权值的对应关系，最终生成矩阵
                this.dgvMatrix.DataSource = _OrgMatrixEle;
                this.dgvMatrix.Update();
            }
            else
            {
                MessageBox.Show(this, "对不起，请先查询出数据集！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }*/
        /*
        private void FillMatrixMap(Boolean isRate)
        {
            // 带权值的计算
            Decimal calValue = new Decimal();
            foreach (Keyword keyword in _OrgKeywordList)
            {
                // 带权值的计算
                if (isRate)
                {
                    calValue = Convert.ToDecimal(keyword.RightRate * keyword.TotalCount);
                }
                else
                {
                    calValue = Convert.ToDecimal(keyword.TotalCount);
                }
                // 填充所需要的Hash表内容
                if (_MatrixMap.ContainsKey(keyword.Name))
                {
                    Decimal preValue = Convert.ToDecimal(_MatrixMap[keyword.Name]);
                    _MatrixMap.Remove(keyword.Name);
                    _MatrixMap.Add(keyword.Name, calValue + preValue);
                }
                else
                {
                    _MatrixMap.Add(keyword.Name, calValue);
                }
            }
        }*/
        /// <summary>
        /// 重置该表单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReset_Click(object sender, EventArgs e)
        {
            // 重置该表单
            this.txtAuthorName.Text = String.Empty;
            this.txtFrequency.Text = "0";
            this.txtInsititution.Text = String.Empty;
            this.txtYear.Text = String.Empty;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            //if (this._ExcelTable != null && this._ExcelTable.Rows.Count > 0)
            //{
            DialogResult result = this.fbdExportPath.ShowDialog();
            if (result == DialogResult.OK)
            {
                String path = this.fbdExportPath.SelectedPath;
                Microsoft.Office.Interop.Excel.Application
                    xApp = ExcelUtil.ExportExcel(this._ExcelTable, "关键字", ref path);
                MessageBox.Show(this, "您的Excel已经导出成功！其路径为：" + path, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                // 导出为Excel的表格
            }
            // }
            // else
            // {
            //    MessageBox.Show(this, "对不起，请先生成矩阵，再设置导出！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //  }
        }
        /// <summary>
        /// 查询Keyword的结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                Int64 frequency = Convert.ToInt64(this.txtFrequency.Text);
                _OrgKeywordList = this.keywordBLL.QueryByCondition(
                    this.txtAuthorName.Text,
                    this.txtInsititution.Text,
                    frequency,
                    this.txtYear.Text);
                this.dgvMatrix.DataSource = _OrgKeywordList;
                this.dgvMatrix.Update();
            }
            catch (FormatException)
            {
                MessageBox.Show(this, "对不起，频次信息不能为非数字，请重新输入频次！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        /// <summary>
        /// 生成矩阵
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (this._OrgKeywordList != null && this._OrgKeywordList.Count > 0)
            {
                _ExcelTable = MatrixUtil.CalculateMatrix(this._OrgKeywordList, this._IsRate);
                this.dgvMatrix.DataSource = _ExcelTable;
                this.dgvMatrix.Update();
            }
            else
            {
                MessageBox.Show(this, "对不起，请先查询，生成线性结果！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void fbdExportPath_HelpRequest(object sender, EventArgs e)
        {

        }
    }
}
